/**
 * 
 */
var _url = "service.do?s=login";

$(function(){
	refreshIndexPage();

	setLogout();
	
	// 添加登陆点击事件
	$("#btnLogin").bind("click", loginOp);
	
	// 注销
	$("#logout").bind("click", function(){
		$.post(_url,{op:"logout"},function(data){
			var event = eval("(" + data + ")");
			if(event.returnCode == "success") {
				setLogout();
			}
		},"html");
	});
	
	// 进入管理
	$("#btnEnter").bind("click", function(){
		if($("#activitySelect option").length > 0) {
			$.post(_url,
				{
					op:"changeActivity",
					index:$("#activitySelect").get(0).selectedIndex
				},
				function(data){
					var event = eval("(" + data + ")");
					if(event.returnCode == "success") {
						window.location.href="adpmgt.jsp";
				}
			},"html");
		} else {
			window.location.href="userinfo.jsp";
		}
		
	});

	// 检查是否登陆
	$.post(_url,{op:"CheckLogin"},function(data){
		var event = eval("(" + data + ")");
		if(event.returnCode == "success"){
			setLogin(event.result);
		} else {
//			alert("未登录");
		}
	},"html");
	
//	$("#loginForm").validate({
//		rules: {
//			username: { required: true },
//			passwd: { required: true }
//		},
//		errorPlacement: function(error, element){
//			error.appendTo(element.siblings("span"));
//		}
//	});

});

// 验证表单填写
var validateInput = function(){
	var u = $.trim($("#username").val());
	var p = $.trim($("#passwd").val());
	if(u.length == 0 || p.length == 0) {
		alert("用户名或密码不能为空！");
		return false;
	}
	return true;
};

// 登陆
var loginOp = function() {
	if(validateInput()){
		var data = {
			op:"login",
			username:$("#username").val(),
			passwd:$("#passwd").val()
		};
		$.post(_url,data,function(data){
			var event = eval("(" + data + ")");
			if(event.returnCode == "success"){
				setLogin(event.result);
			} else if(event.errorMessage != null && event.errorMessage != "") {
				alert(event.errorMessage);
				$("#passwd").val("");
			} else {
				alert("登陆失败");
				$("#passwd").val("");
			}
		},"html");
	} else {
//		alert("验证失败");
	}
}
// 显示用户登陆状态
var setLogin = function(obj){
	$("#loginPanel").css("display", "none");
	$("#loginedInfo").css("display", "block");
	$("#currentUser").html(obj.userName);
	$("#activitySelect").get(0).options.length = 0;
	$.each(obj.activityInfoList, function(index, item){
		$("#activitySelect").get(0).options.add(new Option(item.activityName, item.activityId));
	});
	var activityInfo = obj.activityInfo;
	$("#activitySelect option").each(function(){
		if($(this).val() == activityInfo.activityId) {
			$(this).attr("selected", "true");
		}
	});
}

// 显示登陆界面
var setLogout = function() {
	$("#loginPanel").css("display", "block");
	$("#loginedInfo").css("display", "none");
	$("#username").val("");
	$("#passwd").val("");
}

var refreshIndexPage = function(){
	$.get("other/indexData.xml", {stamp:Math.random()}, function(data){
		// 顶部广告
		var topHtml = "";
		var href = $(data).find("topad").attr("href");
		var src = $(data).find("topad").attr("src");
		topHtml = topHtml +
			"<a href='" + href + "'>" +
			"  <img id='img_top' class='img_top' width='960' height='90' src='" + src + "' />" +
			"</a>";
		$("#mainPage_top").html(topHtml);
		// 循播广告
		var slideshowHtml = "";
		$(data).find("slideshow").find("ad").each(function(i){
			var $ad = $(this);
			var href = $ad.attr("href");
			var src = $ad.attr("src");
			var alt = $ad.attr("alt");
			//alert("href=" + href + ",src=" + src + ",alt=" + alt);
			slideshowHtml = slideshowHtml +
				"<a href='" + href + "' target='_blank'>" +
   				"  <img src='" + src + "' width='630' height='280' alt='" + alt + "'/>" +
   				"</a>";
		});
		$("#slideshow").html(slideshowHtml);
		// slide jquery plugin
		$("#slideshow").slideshow({
			pauseSeconds: 4,
			width:630,
	    	height: 280,
			anchorTarget: '_blank'
		});
		// 公告数据
		$("#noticeContent").html($(data).find("notice").text());
		// 新闻
		var newsHtml = "";
		$(data).find("news").find("item").each(function(i){
			var $item = $(this);
			var href = $item.attr("href");
			var title = $item.attr("title");
			newsHtml = newsHtml +
				"<li class='newsItem'>" +
				"  <a class='newsAnchor' href='" + href + "' title='" + title + "'>" + title + "</a>" +
				"</li>";
			if(i==4) {
				$(".newsInfo:eq(0)").html(newsHtml);
				newsHtml = "";
			}
			if(i==9) {
				$(".newsInfo:eq(1)").html(newsHtml);
				newsHtml = "";
			}
		});
		// 友情链接
		var linkHtml = "";
		$(data).find("links").find("link").each(function(i){
			var $link = $(this);
			var href = $link.attr("href");
			var src = $link.attr("src");
			var title = $link.attr("title");
			linkHtml = linkHtml +
				"<li class='li_link'>" +
				"  <a href='" + href + "' target='_blank'>" +
				"    <img width='80' height='25' src='" + src + "' title='" + title + "'/>" +
				"  </a>" +
				"</li>";
			$("#links").html(linkHtml);
		});
	}, "xml");
}